#include "lineedit.h"
#include <QGuiApplication>
#include"QDebug"
LineEdit::LineEdit(QWidget *parent) :QLineEdit(parent) {
    setAttribute(Qt::WA_InputMethodEnabled, true);
    setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhPreferLowercase);
}

LineEdit::LineEdit(const QString& txt, QWidget *parent) : QLineEdit(txt, parent) {
    //必须在创建自定义文本编辑小部件时设置。
    //setAttribute如果on为真，则设置此小部件的属性属性;否则清除属性。void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on = true)
    setAttribute(Qt::WA_InputMethodEnabled, true);
    //ImhPreferLowercase小写字母优先(但不是必需)。
    //如果设置了Qt::ImhFormattedNumbersOnly标志，输入法可能会改变其可视化组件，以反映只能输入数字。
    setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhPreferLowercase);
}

bool LineEdit::event(QEvent* e) {
    //keyboardRectangle虚拟键盘的几何图形在窗口坐标。 如果不可能知道键盘的几何形状，这可能是一个空矩形。
    const auto keyboard_rect = QGuiApplication::inputMethod()->keyboardRectangle();
    const auto keyboard_visible = QGuiApplication::inputMethod()->isVisible();
    const auto global_y = QWidget::mapToGlobal(rect().topLeft()).y() + height();
    const auto k_global_y = keyboard_rect.topLeft().y();
    const auto diff = k_global_y - global_y;
    const auto need_to_move = diff < 0;

    /* move main widget */
    if (keyboard_visible && !_moved && need_to_move) {
        _moved = true;
        _lastDiff = diff;
        const auto g = parentWidget()->frameGeometry();
        parentWidget()->move(g.x(), g.y() - qAbs(_lastDiff));
    }
    /* roll back */
    if (!keyboard_visible && _moved) {
        _moved = false;
        const auto g = parentWidget()->frameGeometry();
        parentWidget()->move(g.x(), g.y() + qAbs(_lastDiff));
    }
    return QLineEdit::event(e);
}

